package com.qiange.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiange.dao.CategoryDao;
import com.qiange.exception.CustomException;
import com.qiange.pojo.Category;
import com.qiange.pojo.Dish;
import com.qiange.pojo.Setmeal;
import com.qiange.service.CategoryService;
import com.qiange.service.DishService;
import com.qiange.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeById(Long id) {
        LambdaQueryWrapper<Dish> dishLqw = new LambdaQueryWrapper();
        dishLqw.eq(Dish::getCategoryId, id);
        int dishCount = dishService.count(dishLqw);
        if (dishCount > 0) {
            throw new CustomException("当前分类下关联了菜品，不能删除");
        }

        LambdaQueryWrapper<Setmeal> mealLqw = new LambdaQueryWrapper<>();
        mealLqw.eq(Setmeal::getCategoryId, id);
        int mealCount = setmealService.count(mealLqw);
        if (mealCount > 0) {
            throw new CustomException("当前分类下关联了套餐，不能删除");
        }

        super.removeById(id);
    }
}
